package com.zcx.project.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.zcx.atom.util.UploadUtils;
import com.zcx.project.dto.ZcxProblemHandleDto;
import com.zcx.project.entity.ZcxProblem;
import com.zcx.project.entity.ZcxTrainInfo;
import com.zcx.project.mapper.ZcxProblemMapper;
import com.zcx.project.mapper.ZcxTrainInfoMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zcx.project.entity.ZcxProblemHandle;
import com.zcx.project.mapper.ZcxProblemHandleMapper;


@Controller
@RequestMapping("/ZcxProblemHandle")
public class ZcxProblemHandleController {

    @Autowired
    ZcxProblemHandleMapper zcxProblemHandleMapper;

    @Autowired
    ZcxProblemMapper zcxProblemMapper;


    @Autowired
    ZcxTrainInfoMapper zcxTrainInfoMapper;

    @RequestMapping(value = "ZcxProblemHandleHtml")
    public ModelAndView getMainPage(HttpSession session, HttpServletRequest request) {
        ModelAndView mv = new ModelAndView("zcxProblemHandle");
        return mv;
    }

    @ResponseBody
    @RequestMapping(value = "/findList")
    public List findList(ZcxProblemHandle obj) {
        List list = zcxProblemHandleMapper.findList(obj);
        return list;
    }

    @ResponseBody
    @RequestMapping(value = "/query")
    public Map<String, Object> findZcxProblemHandlePage(int currentPage, int rows, ZcxProblemHandleDto obj) {

        //使用分页插件,核心代码就这一行，页数、每页行数
        PageHelper.startPage(currentPage, rows);
        List<ZcxProblemHandle> list = zcxProblemHandleMapper.findList(obj);
        PageInfo<ZcxProblemHandle> pageInfo = new PageInfo<ZcxProblemHandle>(list);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("total", pageInfo.getTotal());
        result.put("rows", pageInfo.getList());
        return result;


    }


    @ResponseBody
    @RequestMapping(value = "/insertZcxProblemHandle")
    public Map<String, Object> addZcxProblemHandle(ZcxProblemHandle obj) {


        ZcxProblem zp = new ZcxProblem();
        zp.setId(obj.getProblemId());
        zp.setProblemAudit(obj.getProblemAudit());
        zcxProblemMapper.updateByPrimaryKeySelective(zp);

        ZcxProblem zcxProblem = zcxProblemMapper.selectByPrimaryKey(obj.getProblemId());


        Map<String, Object> returnMap = new HashMap<String, Object>();
        int i = 1;
        if (obj.getUserId() != null) {
            i = zcxProblemHandleMapper.insertSelective(obj);


            ZcxTrainInfo zcxTrainInfo = new ZcxTrainInfo();

            zcxTrainInfo.setId(zcxProblem.getTrainInfoId());
            zcxTrainInfo.setStatus("维修中");
            zcxTrainInfoMapper.updateByPrimaryKeySelective(zcxTrainInfo);

        }
        if (i == 0) {
            returnMap.put("showInfo", "添加失败！");
        } else {
            returnMap.put("showInfo", "添加成功！");
        }


        return returnMap;
    }


    @ResponseBody
    @RequestMapping(value = "/updateZcxProblemHandle")
    public Map<String, Object> updateZcxProblemHandle(ZcxProblemHandle obj, @RequestParam("files") MultipartFile[] files) {

        String path = UploadUtils.uploadFile(files);
        if (!path.equals("")) {
            obj.setPic(path);
        }

        int i = zcxProblemHandleMapper.updateByPrimaryKeySelective(obj);
        Map<String, Object> returnMap = new HashMap<String, Object>();
        if (i == 0)
            returnMap.put("showInfo", "修改失败！");
        else {
            ZcxProblemHandle zcxProblemHandle = zcxProblemHandleMapper.selectByPrimaryKey(obj.getId());

            ZcxProblem zcxProblem = zcxProblemMapper.selectByPrimaryKey(zcxProblemHandle.getProblemId());


            ZcxTrainInfo zcxTrainInfo1 = zcxTrainInfoMapper.selectByPrimaryKey(zcxProblem.getTrainInfoId());
            ZcxTrainInfo zcxTrainInfo = new ZcxTrainInfo();

            zcxTrainInfo.setId(zcxProblem.getTrainInfoId());
            zcxTrainInfo.setStatus("正常");
            zcxTrainInfo.setIsUpdate(zcxTrainInfo1.getIsUpdate() + 1);
            zcxTrainInfoMapper.updateByPrimaryKeySelective(zcxTrainInfo);
            returnMap.put("showInfo", "修改成功！");
        }
        return returnMap;
    }

    @ResponseBody
    @RequestMapping(value = "/deleteZcxProblemHandle")
    public Map<String, Object> deleteZcxProblemHandle(int id) {
        int i = zcxProblemHandleMapper.deleteByPrimaryKey(id);
        Map<String, Object> returnMap = new HashMap<String, Object>();
        if (i == 0)
            returnMap.put("showInfo", "删除失败！");
        else
            returnMap.put("showInfo", "删除成功！");
        return returnMap;
    }


}
